package edu.purdue.cs.ds.vss;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;

public class Share implements Serializable{

	/**
	 * For the compiler
	 */
	private static final long serialVersionUID = -7558999378422273030L;

	/**
	 * The parent secret that this share is a part of
	 */
	private ParentIndex parentIndex;
	
	/**
	 * Share value
	 */
	private BigInteger s;
	
	/**
	 * Random value
	 */
	private BigInteger r;
	
	private ArrayList<BigInteger> lambda;
	
	private ArrayList<BigInteger> a;
	
	/**
	 * Index of the share
	 */
	private int index;
	
	public Share(BigInteger secret, int index, ParentIndex parentIndex) {
		this.s = secret;
		this.parentIndex = parentIndex;
		this.index = index;
	}
	
	public Share(BigInteger secret, BigInteger rval, int index, ParentIndex parentIndex) {
		this.r = rval;
		this.s = secret;
		this.parentIndex = parentIndex;
		this.index = index;
	}
	
	public Share(String shareVal) {
		int sepPos = shareVal.indexOf('|');
		this.parentIndex = new ParentIndex(shareVal.substring(0, sepPos));
		shareVal = shareVal.substring(sepPos+1);
		sepPos = shareVal.indexOf(':');
		String tmp = shareVal.substring(0, sepPos);
		this.index = Integer.parseInt(tmp);
		tmp = shareVal.substring(sepPos+1);
		this.s = new BigInteger(tmp);
	}

	public BigInteger getS() {
		return s;
	}
	
	public void setS(BigInteger sval){
		s = sval;
	}
	
	public BigInteger getR(){
		return r;
	}
	
	public void setR(BigInteger rp){
		r = rp;
	}

	public ParentIndex getParentIndex() {
		return parentIndex;
	}

	public int getIndex() {
		return index;
	}
	
	@Override
	public String toString() {
		String val = this.parentIndex.toString() + "\n ["  + this.index + "] : "+ this.s; 
		return val;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Share) {
			Share tmp = (Share) obj;
			return tmp.getS().equals(this.s)&& tmp.getIndex() == this.index 
				&& tmp.getParentIndex().equals(this.parentIndex);
		} else {
			return false;
		}
	}
	
	public String toDisk() {
		String val = "";
		String piStr = this.parentIndex.toDisk();
		val += piStr;
		val += "|";
		val += this.index + ":" + this.s.toString();
		return val;
	}

	public ArrayList<BigInteger> getLambda() {
		
		if(lambda == null)
			return null;
		
		ArrayList<BigInteger> ltmp = new ArrayList<BigInteger>();
		for(int i = 0; i < lambda.size(); ++i)
			ltmp.add(lambda.get(i));
		
		return ltmp;
	}

	public void setLambda(ArrayList<BigInteger> lambda) {
		this.lambda = lambda;
	}

	public ArrayList<BigInteger> getA() {
		
		if(a == null)
			return null;
		
		ArrayList<BigInteger> atmp = new ArrayList<BigInteger>();
		for(int i = 0; i < a.size(); ++i)
			atmp.add(a.get(i));
		
		
		return atmp;
	}

	public void setA(ArrayList<BigInteger> a) {
		this.a = a;
	}
	

}
